﻿using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Conference.Core.BL.Sync;
using Conference.Core.DL;
using Conference.Core.Dal;
using TeaLightV5.Foundation.MvvmCross.Application;

namespace Conference.Core.UX
{
    public class App 
        : TeaApplication
        , IMvxServiceProducer
        //, IMvxServiceProducer<IRepositoryBase>
    {
        public App()
        {
            InitialisePlugins();
            InitialiseText();
            InitialiseServices();
            InitaliseErrorSystem();

            // set up the model
            //var dataStore = new SimpleDataStore();
            //this.RegisterServiceInstance<IDataStore>(dataStore);

            // todo
            Database.CreateTables();
            Repositories.Register();

            SyncContext.Synchronize();

            // set the start object
            StartApplicationObject startApplicationObject = new StartApplicationObject();
            this.RegisterServiceInstance<IMvxStartNavigation>(startApplicationObject);
        }

        private void InitialisePlugins()
        {
            Cirrious.MvvmCross.Plugins.Sqlite.PluginLoader.Instance.EnsureLoaded();

            //Cirrious.MvvmCross.Plugins.File.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.JsonLocalisation.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.ResourceLoader.PluginLoader.Instance.EnsureLoaded();

            //// these don't really need to be loaded on startup, but it's convenient for now
            //Cirrious.MvvmCross.Plugins.Email.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.PhoneCall.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.Share.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.Visibility.PluginLoader.Instance.EnsureLoaded();
            //Cirrious.MvvmCross.Plugins.WebBrowser.PluginLoader.Instance.EnsureLoaded();
        }

        private void InitaliseErrorSystem()
        {
            //var errorHub = new ErrorApplicationObject();
            //this.RegisterServiceInstance<IErrorReporter>(errorHub);
            //this.RegisterServiceInstance<IErrorSource>(errorHub);
        }

        private void InitialiseServices()
        {
            //var repository = new ConferenceService();
            //Cirrious.MvvmCross.Plugins.File.PluginLoader.Instance.EnsureLoaded();
            //this.RegisterServiceInstance<IConferenceService>(repository);
            //this.RegisterServiceInstance<ITwitterSearchProvider>(new TwitterSearchProvider());
        }

        private void InitialiseText()
        {
            //var builder = new TextProviderBuilder();
            //// TODO - could choose a language here: builder.LoadResources(whichLanguage);
            //this.RegisterServiceInstance<IMvxTextProvider>(builder.TextProvider);
        }
    }
}